<div id="admin-links">
	<a href="index.php?s=league-management">Leagues Management</a>
	<a href="index.php?s=league-management&a=1">New League</a>		
</div>

<div id="content">
<?php
	$a = REPLACE_Injection($_GET["a"]);
	if($a == 1){
		?>
		<h1>League Creation</h1>
		<form action="index.php?s=league-management&a=2" method="post">
		<table border="0" cellspacing="0" cellpadding="0" class="form">
			<tr>
				<td><strong>League Name</strong></td>
				<td><input type="text" maxlength="30" size="50" name="name" /></td>				
			</tr>
			<tr>
				<td><strong>Secondary</strong></td>
				<td>
					<select name="secondary">
						<option value="0">No</option>
						<option value="1">Yes</option>
					</select>
				</td>				
			</tr>
			<tr><td colspan="2"><input type="submit" value="Create League" class="boton"/></td></tr>
		</table>
		</form>
		<?php
	}else if($a == 2){
		if(!$nombre = REPLACE_Injection($_POST["name"]))
			$error = "The league name is not an optional fileld";
		$secundaria = REPLACE_Injection($_POST["secondary"]);
		
		if(sizeof($error) == 0){
			$values = array(NULL, $nombre, $secundaria);
			$insert = new mysqlInsert("tournament", "", "ligas", $values);
			$insert->execute();
			?>
			<h1>League Creation</h1>
			<div id="ok">The league has been created.</div>
			<a href="index.php?s=league-management" class="back">Back to Management</a>
			<?php
		}else{
			?>			
			<h1>League Creation</h1>
			<div id="error">You have the following errors: <br><br><?php echo $error;?></div>
			<a href="index.php?s=league-management&a=1" class="back">Back to League Creation</a>
			<?php
		}
	}else if($a == 3){
		?>
		<h1>League Edition</h1>
		<?php
			$id = REPLACE_Injection($_GET["id"]);
			$query = new mysqlQuery("tournament", "", "ligas", "", "`id`='".$id."'", "", "");
			$query->execute();
			if($query->getCount() == 1){
				$row = $query->next();
				$nombre = $row["nombre"];
				?>
				<form action="index.php?s=league-management&a=4&id=<?php echo $id;?>" method="post">
				<table border="0" cellspacing="0" cellpadding="0" class="form">
					<tr>
						<td><strong>League Name</strong></td>
						<td><input type="text" maxlength="30" size="50" name="name" value="<?php echo $nombre;?>" /></td>
					</tr>					
					<tr>
						<td><strong>Secondary</strong></td>
						<td>
							<select name="secondary">
								<option value="0" <?php if($row["secondary"] == '1'){echo "selected";}?>>No</option>
								<option value="1" <?php if($row["secondary"] == '1'){echo "selected";}?>>Yes</option>
							</select>
						</td>				
					</tr>
					<tr><td colspan="2"><input type="submit" value="Edit League" class="boton"/></td></tr>
				</table>
				</form>
				<?php
			}else{
				echo '<div id="error">League does not exist</div>';
			}
	}else if($a == 4){
		$id = REPLACE_Injection($_GET["id"]);		
		if(!$nombre = REPLACE_Injection($_POST["name"]))
			$error = "The league name is not an optional fileld";
		$secundaria = REPLACE_Injection($_POST["secondary"]);
			
		if(sizeof($error) == 0){
			$values = array("nombre", "secundaria");
			$data = array($nombre, $secundaria);
			$update = new mysqlUpdate("tournament", "", "ligas", $values, $data, "`id`='".$id."'");
			$update->execute();
			?>
			<h1>League Edition</h1>
			<div id="ok">The league has been modified..</div>
			<a href="index.php?s=league-management" class="back">Back to Management</a>
			<?php
		}else{
			?>			
			<h1>League Edition</h1>
			<div id="error">You have the following errors: <br><br><?php echo $error;?></div>
			<a href="index.php?s=league-management&a=3&id=<?php echo $id;?>" class="back">Back to League Edition</a>
			<?php
		}
	}else{
		?>
		<h1>Leagues Management</h1>
		
		<table border="0" cellspacing="0" cellpadding="0" class="table">
			<tr>
				<td class="title">League Name</td>
				<td class="title">Secondary</td>
				<td class="title" colspan="1">Operations</td>
			</tr>
			<?php 
				$query = new mysqlQuery("tournament", "", "ligas", "", "", "id", "ASC");
				$query->execute();
				$n = 0;
				while($row = $query->next()){
					echo '<tr>';
					echo '<td class="td'.(($n%2)+1).'"><strong>'.$row["nombre"].'</strong></td>';
					if($row["secundaria"] == '1')
						echo '<td class="td'.(($n%2)+1).'">YES</strong></td>';
					else 
						echo '<td class="td'.(($n%2)+1).'">&nbsp;</strong></td>';
					echo '<td class="td'.(($n%2)+1).'"><a href="index.php?s=league-management&a=3&id='.$row["id"].'">Edit</a></td>';
					//echo '<td class="td'.(($n%2)+1).'"><a href="index.php?s=league-management&a=5&id='.$row["id"].'">Delete</a></td>';
					echo '</tr>';
					$n++;
				}				
			?>
		</table>
		<?php 
	}
?>
</div>